package BlogMybatis.blog.controller.admin;



import BlogMybatis.blog.mapper.BlogMapper;
import BlogMybatis.blog.pojo.*;
import BlogMybatis.blog.service.BlogService;
import BlogMybatis.blog.service.TagService;
import BlogMybatis.blog.service.TypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Brandt
 * @since 2020-06-07
 */
@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;

    //后台博客列表
    @GetMapping("/blogs")
    public String blogs(@RequestParam(required = false,defaultValue = "1") int pageNum,Model model){
        String orderBy = "update_time desc";
        PageHelper.startPage(pageNum,6,orderBy);
        List<Blog> allBlog = blogService.getAllBlog();
        PageInfo pageInfo = new PageInfo(allBlog);
        List<Type> types = typeService.list();
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("types",types);
        return"admin/blogs";
    }

    //后台按条件查询博客
    @PostMapping("/blogs/search")
    public String searchBlogs(Blog blog,@RequestParam(required = false,defaultValue = "1") int pageNum,
                              Model model){
        String orderBy = "update_time desc";
        PageHelper.startPage(pageNum,6,orderBy);
        List<Blog> blogList = blogService.searchAllBlog(blog);
        PageInfo pageInfo = new PageInfo(blogList);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/blogs :: blogList";
    }

    //去博客新增页面
    @GetMapping("/blogs/input")
    public String toAddBlog(Model model){
        Blog blog = new Blog();
        model.addAttribute("blog",blog);
        List<Type> types = typeService.list();
        List<Tag> tags = tagService.list();
        model.addAttribute("types",types);
        model.addAttribute("tags",tags);
        return "admin/blogs-input";
    }

    //(新增，修改)博客
    @PostMapping("/blogs")
    public String addBlog(Blog blog, HttpSession session, RedirectAttributes attributes){
        //设置user属性
        blog.setUser((User)session.getAttribute("user"));
        //设置发布博客的用户id
        blog.setUserId(blog.getUser().getId());
        //设置blog中的type
        blog.setType(typeService.getById(blog.getType().getId()));
        //设置blog中的typeId
        blog.setTypeId(blog.getType().getId());
        //给blog中的list<Tag>赋值
        blog.setTags(tagService.getTagByString(blog.getTagIds()));
        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());
        if(blog.getId() == null){
            blogService.save(blog);
            List<Tag> tags = blog.getTags();
            BlogAndTags blogAndTags = null;
            for (Tag tag : tags) {
                blogAndTags = new BlogAndTags(tag.getId(),blog.getId());
                blogMapper.saveBlogAndTag(blogAndTags);
            }
            attributes.addFlashAttribute("message","新增成功");
        }else {
         blogService.updateBlog(blog);
            attributes.addFlashAttribute("message","修改成功");
        }
        return "redirect:/admin/blogs";
    }

    //根据某个博客id跳到编辑页面
    @GetMapping("/blogs/{id}/input")
    public String toEditBlog(@PathVariable Long id,Model model){
        Blog blog = blogService.getByBlog(id);
        blog.init();  //将tags集合转换成tagIds字符串
        List<Type> typeList = typeService.list();
        List<Tag> tagList = tagService.list();
        model.addAttribute("blog",blog);
        model.addAttribute("types",typeList);
        model.addAttribute("tags",tagList);
        return "admin/blogs-input";
    }

    @GetMapping("/blogs/{id}/delete")
    public String deleteBlog(@PathVariable("id") Long id,RedirectAttributes attributes){
        blogService.removeById(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/blogs";
    }

}

